<template>
    <div v-show="showDialog">
        <div class="add-address-show">
            <div class="title">
                <div>修改订单名称</div>
                <img :src="$global.Image.icon_close" @click="_cancelDialog" />
            </div>
            <div class="add-input">
                <el-form :model="addressForm" :rules="rules" ref="addressForm" label-width="100px" class="rule-form">
                    <el-form-item label="原名称" prop="old_name">
                        <el-input v-model="addressForm.old_name" placeholder="原名称" readonly></el-input>
                    </el-form-item>
                    <el-form-item label="新名称" prop="order_name">
                        <el-input v-model="addressForm.order_name" placeholder="请输入新名称"></el-input>
                    </el-form-item>
                    <div class="td-fbc" style="margin-left: 100px;">
                        <el-button class="btn-b" @click="_cancelDialog">取消</el-button>
                        <el-button class="btn-r" type="primary" style="color:#FFFFFF !important" @click="submitForm('addressForm')">确认</el-button>
                    </div>
                </el-form>
            </div>
        </div>
        <div class="dialog-shade"></div>
    </div>
</template>

<script>
import { getAddress, postAddress } from "@api/user";
import { getCity } from "@api/public";
import { purchaseOrderName, updateOrder } from "@api/order";

export default {
    name: "orderName",
    props: ["item"],
    data() {
        return {
            showDialog: false,
            addressForm: {
                old_name: "",
                order_name: "",
            },
            rules: {
                order_name: [
                    {
                        required: true,
                        message: "请输入订单名称",
                        trigger: "blur",
                    },
                ],
            },
            callback: null,
        };
    },
    methods: {
        // 打开编辑
        open (item,callback) {
            console.log(item)
            this.init()
            this.callback = callback;
            this.orderInfo = item
            this.addressForm.old_name = item.order_name
            this.showDialog = true;
        },
        // 初始化
        init() {
            this.addressForm.old_name = "";
            this.addressForm.order_name = "";
        },
        _cancelDialog() {
            this.init()
            this.showDialog = false;
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        async submit() {
            let { order_name } = this.addressForm;
            try {
                let that = this,
                    params = {
                        order_name: order_name,
                        edit_type: 1,
                    };
                    params.id = this.orderInfo.id
                updateOrder(params)
                    .then(function (res) {
                        if (res.status != 200) {
                            that.$dialog.toast({ mes: res.msg });
                            return;
                        }
                        that.$dialog.toast({ mes: "修改成功" });
                        that.showDialog = false;
                        that.callback();
                    })
                    .catch((err) => {
                        that.$dialog.error(err.msg);
                    });
            } catch (e) {
                this.$dialog.error(e.msg);
            }
        },
    },
};
</script>
<style >
.el-input {
    width: 100%;
}
.add-address-show .rule-form{
    width: auto;
}
</style>
<style scoped  rel="stylesheet/scss" lang="scss">
$color: #da213d;
.for-itm {
    margin-left: -50px;
    margin-top: 50px;
    .el-form-item__content {
    margin-left: 0 !important;
    width: 540px;
    display: flex;
    justify-content: space-between;
    }
    .btn-r {
        width: 240px;
        height: 44px;
        background: #da213d;
        font-size: 16px;
        border-radius: 5px;
    }
    .btn-b {
        width: 242px;
        height: 46px;
        border: 1px solid #da213d !important;
        border-radius: 5px;
        font-size: 16px;
        color: #DA213D !important;
        background: #fff !important;
    }
}
.add-address-show {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 200px;
    width: 700px;
    height: 370px;
    border-radius: 4px;
    background-color: rgba(255, 255, 255, 1);
    box-shadow: 0 0 15px 4px rgba(0, 0, 0, 0.2);
    margin: auto;
    z-index: 20;
    .title {
        display: flex;
        width: 100%;
        height: 67px;
        align-items: center;
        background: #fbf0f0;
        justify-content: center;
        > div:nth-of-type(1) {
            font-size: 21px;
            color: #da213d;
            font-weight: bold;
        }
        img {
            display: block;
            width: 20px;
            height: 20px;
            font-size: 30px;
            color: #999999;
            position: absolute;
            right: 32px;
            cursor: pointer;
        }
    }
    .add-input {
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        padding: 60px 0;
    }
    .rule-form {
        width: 460px;
        button {
            width: 140px;
        }
    }
}

.dialog-shade {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 18;
    background-color: rgba($color: #000000, $alpha: 0.4);
}

.el-cascader {
    display: block;
    position: absolute;
    top: 0;
    width: 100%;
    opacity: 0;
}
</style>